﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;

namespace FileArchive
{
    public class DropShadow
    {
        private const int shadowSize = 5;

        // static for good perfomance 
        static Image shadowDownRight = new Bitmap(typeof(DropShadow), "Images.tshadowdownright.png");
        static Image shadowDownLeft = new Bitmap(typeof(DropShadow), "Images.tshadowdownleft.png");
        static Image shadowDown = new Bitmap(typeof(DropShadow), "Images.tshadowdown.png");
        static Image shadowRight = new Bitmap(typeof(DropShadow), "Images.tshadowright.png");
        static Image shadowTopRight = new Bitmap(typeof(DropShadow), "Images.tshadowtopright.png");
        static Pen BorderPen = new Pen(Color.FromArgb(15, 0,0,0));
        public static Rectangle GetContentRect(Rectangle rect)
        {
            return new Rectangle(rect.X + 1, rect.Y + 1, rect.Width - shadowSize - 1, rect.Height - shadowSize - 1);
        }
        public static void DrawRectangle(Graphics g, Rectangle rect)
        {
            // Create tiled brushes for the shadow on the right and at the bottom.
            TextureBrush shadowRightBrush = new TextureBrush(shadowRight, WrapMode.Tile);
            TextureBrush shadowDownBrush = new TextureBrush(shadowDown, WrapMode.Tile);

            // Translate (move) the brushes so the top or left of the image matches the top or left of the
            // area where it's drawed. If you don't understand why this is necessary, comment it out. 
            // Hint: The tiling would start at 0,0 of the control, so the shadows will be offset a little.
            shadowDownBrush.TranslateTransform(rect.X, rect.Bottom - shadowSize);
            shadowRightBrush.TranslateTransform(rect.Right - shadowSize, rect.Y);

            // Define the rectangles that will be filled with the brush.
            // (where the shadow is drawn)
            Rectangle shadowDownRectangle = new Rectangle(
                rect.X + shadowSize,                      // X
                rect.Bottom - shadowSize,                            // Y
                rect.Width - (shadowSize * 2),        // width (stretches)
                shadowSize                                      // height
                );                                    
           
            Rectangle shadowRightRectangle = new Rectangle(
                rect.Right - shadowSize,                             // X
                rect.Top+shadowSize,                      // Y
                shadowSize,                                     // width
                rect.Height - (shadowSize * 2)        // height (stretches)
                );

            // And draw the shadow on the right and at the bottom.
            g.FillRectangle(shadowDownBrush, shadowDownRectangle);
            g.FillRectangle(shadowRightBrush, shadowRightRectangle);

            // Now for the corners, draw the 3 5x5 pixel images.
            g.DrawImage(shadowTopRight, new Rectangle(rect.Right - shadowSize, rect.Top, shadowSize, shadowSize));
            g.DrawImage(shadowDownRight, new Rectangle(rect.Right - shadowSize, rect.Bottom - shadowSize, shadowSize, shadowSize));
            g.DrawImage(shadowDownLeft, new Rectangle(rect.Left, rect.Bottom - shadowSize, shadowSize, shadowSize));

            // Draw Top and Left borders
            //g.DrawLine(BorderPen, rect.X, rect.Y, rect.Right - shadowSize, rect.Y);
            //g.DrawLine(BorderPen, rect.X, rect.Y, rect.X, rect.Bottom - shadowSize);
            g.DrawRectangle(BorderPen, rect.X, rect.Y, rect.Width - shadowSize, rect.Height - shadowSize);
            // Memory efficiency
            shadowDownBrush.Dispose();
            shadowRightBrush.Dispose();

            shadowDownBrush = null;
            shadowRightBrush = null;
        } 
    }
}
